#!/bin/bash

cd /etc/letsencrypt/live/
email='{{EMAIL}}'
path='{{SSL_PATH}}'

command='certbot certonly --standalone'

for i in $(ls -d */ |awk -F"/" '{print $1}'); do
        echo $i
        command+=" -d "$i
done

command+=" --non-interactive --agree-tos --email $email --http-01-port=8888"

bash -c "$command"

for i in $(ls -d */ |awk -F"/" '{print $1}'); do
        bash -c "cat /etc/letsencrypt/live/$i/fullchain.pem /etc/letsencrypt/live/$i/privkey.pem > $path/$i.pem"
done

# Reload  HAProxy
sudo systemctl reload haproxy